package com.totoro.domain.key;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Embeddable;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import com.totoro.domain.Notice;
import com.totoro.domain.Role;


/*
 * 定义Notice-Role关联表的主键实体
 */
@Embeddable
public class NoticeRoleKey implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/*
	 * Field
	 */
	// 映射Role为主键
	@ManyToOne(cascade = { CascadeType.ALL })
	@JoinColumn(name = "role_id", referencedColumnName = "id")
	private Role role;// 角色

	// 映射Resource为主键
	@ManyToOne(cascade = { CascadeType.ALL })
	@JoinColumn(name = "notice_id", referencedColumnName = "id")
	private Notice notice;// 通知

	// 构造空构造器
	public NoticeRoleKey() {
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public Notice getNotice() {
		return notice;
	}

	public void setNotice(Notice notice) {
		this.notice = notice;
	}

	// 主键实体类必须复写hashCode方法
	@Override
	public int hashCode() {
		return getNotice().getId() * 13 + getRole().getNo().hashCode();
	}

	// 主键实体类必须复写equals方法
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj != null && this.getClass() == obj.getClass()) {
			NoticeRoleKey target = (NoticeRoleKey) obj;
			if (getNotice().getId().equals(target.getNotice().getId())
					&& getRole().getNo().equals(target.getRole().getNo()))
				return true;
		}
		return false;
	}
}
